<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.inc.php');
require_once(__PHY_LIB.'/class/uWork.class.php');
require_once(__PHY_LIB.'/class/uUser.class.php');
require_once(__PHY_LIB.'/class/uCategory.class.php');

$rReq = validate($_GET, 'mode:e[none,content]');

$rP = validate($_GET, 'category:u|artist:u|price:u|year:u|sort:e[latest,topviewed,lowprice]');
$oTpl->assign_by_ref('rP', $rP);
$sWhere = "TRUE";

/// 类别：过滤输入 ///
$rOnCtgrys = array();
foreach (explode(',', $rP['category']) as $v) {
	if (!(0 < ($n = intval($v)))) { continue; }
	if (false === ($k = array_search($n, $rOnCtgrys))) { $rOnCtgrys[] = $n; } // 没有则加入
	else { unset($rOnCtgrys[$k]); } // 有了则去掉
}
/// 类别：数据 ///
$oMainCtgryList = new uCategoryList();
$oMainCtgryList->cfgWhere("parent_id=0");
$oMainCtgryList->load();
while ($oMainCtgry = $oMainCtgryList->nextItem()) {
	if (in_array($oMainCtgry->getID(), $rOnCtgrys)) { $oMainCtgry->set('On', true); }
}
$oTpl->assign('rMainCtgrys', $oMainCtgryList->fetchArray());
/// 类别：输出 ///
if (0 < sizeof($rOnCtgrys)) {
	$sWhere .= " AND category_id IN ('".implode("','", $rOnCtgrys)."')";
}
else { $oTpl->assign('bAllCtgrysOn', true); }
$rP['category'] = implode(",", $rOnCtgrys);

/// 艺术家：过滤输入 ///
$rOnArtists = array();
foreach (explode(',', $rP['artist']) as $v) {
	if (!(0 < ($n = intval($v)))) { continue; }
	if (false === ($k = array_search($n, $rOnArtists))) { $rOnArtists[] = $n; } // 没有则加入
	else { unset($rOnArtists[$k]); } // 有了则去掉
}
/// 艺术家：数据 ///
$oUserList = new uUserList();
$oUserList->cfgWhere("role_artist=1");
$oUserList->load();
while ($oUser = $oUserList->nextItem()) {
	if (in_array($oUser->getID(), $rOnArtists)) { $oUser->set('On', true); }
}
$oTpl->assign('rArtists', $oUserList->fetchArray());
/// 艺术家：输出 ///
if (0 < sizeof($rOnArtists)) {
	$sWhere .= " AND owner_id IN ('".implode("','", $rOnArtists)."')";
}
else { $oTpl->assign('bAllArtistsOn', true); }
$rP['artist'] = implode(",", $rOnArtists);

/// 价格：过滤输入 ///
$rOnPrices = array();
foreach (explode(',', $rP['price']) as $v) {
	if (!strpos($v, '-')) { continue; }
	if (false === ($k = array_search($v, $rOnPrices))) { $rOnPrices[] = $v; } // 没有则加入
	else { unset($rOnPrices[$k]); } // 有了则去掉
}
/// 价格：数据 ///
if ('dollor' == $_SESSION['MONEY']) { // 美元
	$rPrices = array(
		array('name' => '0-35',    'title' => '$0 - $35'),
		array('name' => '36-65',  'title' => '$36 - $65'),
		array('name' => '66-100', 'title' => '$66 - $100'),
		array('name' => '101-160', 'title' => '$101 - $160'),
		array('name' => '161-9999', 'title' => 'above $160'),
	);
}
else { // 人民币
	$rPrices = array(
		array('name' => '0-200',  'title' => '￥0 - ￥200'),
		array('name' => '201-400', 'title' => '￥201 - ￥400'),
		array('name' => '401-600', 'title' => '￥401 - ￥600'),
		array('name' => '601-1000', 'title' => '￥601 - ￥1000'),
		array('name' => '1001-99999', 'title' => '￥1000以上'),
	);
}
foreach ($rPrices as $k => $r) {
	if (in_array($r['name'], $rOnPrices)) { $rPrices[$k]['On'] = true; }
}
$oTpl->assign('rPrices', $rPrices);
/// 价格：输出 ///
if (0 < sizeof($rOnPrices)) {
	$sWhere .= " AND (";
	$sPriceField = 'dollor' == $_SESSION['MONEY'] ? 'price_en' : 'price';
	foreach ($rOnPrices as $sMinMax) {
		list($fMin, $fMax) = explode('-', $sMinMax);
		$sWhere .= "{$sPriceField} BETWEEN '{$fMin}' AND '{$fMax}' OR ";
	}
	$sWhere = substr($sWhere, 0, -4).")"; // 去掉最后的" OR "
}
else { $oTpl->assign('bAllPricesOn', true); }
$rP['price'] = implode(",", $rOnPrices);

/// 年份：过滤输入 ///
$rOnYears = array();
foreach (explode(',', $rP['year']) as $v) {
	if (4 != strlen($v)) { continue; }
	if (false === ($k = array_search($v, $rOnYears))) { $rOnYears[] = $v; } // 没有则加入
	else { unset($rOnYears[$k]); } // 有了则去掉
}
/// 年份：数据 ///
$rYears = array(
	array('name' => '2012', 'title' => '2012'),
	array('name' => '2011', 'title' => '2011'),
	array('name' => '2010', 'title' => '2010'),
	array('name' => '2009', 'title' => '2009'),
	array('name' => '2008', 'title' => '2008'),
	array('name' => '2007', 'title' => '2007'),
	array('name' => '2006', 'title' => '2006'),
	array('name' => '2005', 'title' => '2005'),
);
foreach ($rYears as $k => $r) {
	if (in_array($r['name'], $rOnYears)) { $rYears[$k]['On'] = true; }
}
$oTpl->assign('rYears', $rYears);
/// 年份：输出 ///
if (0 < sizeof($rOnYears)) {
	$sWhere .= " AND (";
	foreach ($rOnYears as $sYear) {
		$sWhere .= "year='".addslashes($sYear)."' OR ";
	}
	$sWhere = substr($sWhere, 0, -4).")"; // 去掉最后的" OR "
}
else { $oTpl->assign('bAllYearsOn', true); }
$rP['year'] = implode(",", $rOnYears);

/// 作品列表 ///
$oWorkList = new uWorkList();
$oWorkList->cfgWhere($sWhere);
if ('lowprice' == $rP['sort']) { $oWorkList->cfgOrder('price ASC'); }
elseif ('topviewed' == $rP['sort']) { $oWorkList->cfgOrder('year ASC'); }
else { $oWorkList->cfgOrder('id DESC'); }
$oWorkList->load();
while ($oWork = $oWorkList->nextItem()) {
	$oWork->getSku(0, 0, true);
}
$oTpl->assign('rWorks', $oWorkList->fetchArray());

if ('content' == $rReq['mode']) {
	$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'_content.tpl');
}
else {
	$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
}
?>
